package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.dao.SetmealDao;
import com.example.reggie.domain.Category;
import com.example.reggie.domain.Setmeal;
import com.example.reggie.domain.SetmealDish;
import com.example.reggie.dto.SetMealDto;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     * @param setMealDto
     * @return
     */
    @PostMapping
    public R<String> saveSetMeal(@RequestBody SetMealDto setMealDto) {
        setmealService.saveSetMealWithSetmealDishes(setMealDto);
        return R.success("添加套餐成功！");
    }

    /**
     * 套餐信息展示分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {

        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetMealDto> setMealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setMealDtoPage, "records");


        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        slqw.like(name != null, Setmeal::getName, name).orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, slqw);

        List<Setmeal> setmealPageRecords = setmealPage.getRecords();
        List<SetMealDto> setMealDtoPageRecords = new ArrayList<>();


        for (Setmeal setmealPageRecord : setmealPageRecords) {
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(setmealPageRecord, setMealDto);
            Long categoryId = setmealPageRecord.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setMealDto.setCategoryName(categoryName);
            setMealDtoPageRecords.add(setMealDto);
        }
        setMealDtoPage.setRecords(setMealDtoPageRecords);

        return R.success(setMealDtoPage);
    }

    /**
     * 删除套餐和套餐菜品关系表里的数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithSetmealDish(ids);
        return R.success("删除成功！");
    }


    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        setmealService.changeStatus(status,ids);
        return R.success("修改状态成功！");
    }

    @GetMapping("/{id}")
    public R<SetMealDto> getSetmeal(@PathVariable Long id){
        SetMealDto setMealDto = setmealService.getSetmealDtoById(id);
        return R.success(setMealDto);
    }

    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetMealDto setMealDto){
        setmealService.updateSetmeal(setMealDto);
        return R.success("修改成功！");
    }

    @GetMapping("/list")
    public R<List<SetMealDto>> list(Setmeal setmeal){
        List<SetMealDto> setMealDtoList = new ArrayList<>();
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,categoryId);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);

        for (Setmeal setmeal1 : setmealList) {
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(setmeal1,setMealDto);
            Long id = setmeal1.getId();
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
            List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setMealDto.setSetmealDishes(setmealDishes);
            setMealDtoList.add(setMealDto);
        }

        return R.success(setMealDtoList);

    }

}
